#include "ComposedStructDTD.h"
#include "IdentDTD.h"
#include <iostream>

using namespace std;

void ComposedStructDTD::addComponent(ComponentDTD * c)
{
    components.push_back(c);
}

void ComposedStructDTD::display() const
{

}

void ComposedStructDTD::setCard(Card c)
{
    card = c;
}

vector<string>* ComposedStructDTD::getChildren() const
{
    vector<string> * children = new vector<string>();


    printf("Taille de components : %d\n", components.size());
    vector<ComponentDTD*>::const_iterator it;
    /* for each component of the composed sturcture */
    for( it=components.begin() ; it != components.end(); ++it)
    {
        IdentDTD * id = static_cast<IdentDTD*> (*it);
        ComposedStructDTD *cs = static_cast <ComposedStructDTD *> (*it);

        if ( id != NULL) /* if the component is an ident, we add it to the vector<string> */
        {
            cout<<"C'est un ident : " << id->getName() ;
            children->push_back( id->getName() );
        }

        if( cs != NULL) /* if the component is a composed struct, we call the function recursively */
        {
            printf("C'est une composed struct\n");
            vector<string> * cs_children = cs->getChildren();
            /* we must add to the vector children the children of cs */

            if( ! cs_children->empty() )
            {
                vector<string>::iterator it2;
                for( it2 = cs_children->begin(); it2 != cs_children->end() ; ++it2)
                {
                    children->push_back( *it2 );
                }
            }

            delete(cs_children);
        }

        else
        {
            cout<<"C'est un PCDATA là \n";
        }
    }

    return children;
}
